<?php
/*
 *  Maniter: A webbased business managment tool.
 *  Copyright (C) 2007  Dimitri Michaux <dimitri.michaux@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * purchase.class.php, Maniter
 * 
 * This class is a purchase. You set some basic things like description,
 * amount and the vat, after that it calculates all the other values for
 * you. It needs to id of its parrent.
 *
 * @author Dimitri Michaux
 * @version 0.1
 * @package maniter_classes
 */

class Purchase {
	//number of items
	//integer
	private $_amount;

	//information about the item
	//string
	private $_description;

	//price per piece
	//float
	private $_price;
	
	//vat
	//float
	private $_vat;
	
	private $_id;
	
	private $_pos;

	public function __construct($dbId=-1){
		if( $dbId !== -1 ){
			$this->_id = $dbId;
		}

		$this->_amount = 0;
		$this->_description = "";
		$this->_price = 0;
		$this->_vat = 21/100;
	}
	
	public function setPos( $pos ){
		if( settype( $pos, "int" ) )
			$this->_pos = $pos;

		return $this;
	}
	
	public function getDbId(){
		return $this->_id;
	}
	
	public function setAmount( $amount ) {
		if( settype( $amount, "float" ) )
			$this->_amount = $amount;
		
		return $this;
	}
	
	public function setDescription( $description ) {
		$this->_description = $description;
		
		return $this;
	}
	
	public function setPrice( $price ) {
		if( is_numeric($price) )
			$this->_price = $price;
		
		return $this;
	}
	
	public function setVat( $vat ) {
		if( $vat != "" ) {
			//strip percent sign
			if( $vat{strlen($vat)-1}=="%" )
				$vat{strlen($vat)-1} = "";
			
			//returns false if the string has a non numeric sign in it
			if( settype( $vat, "float" ) )
				$this->_vat = $vat/100;
		}
		
		return $this;
	}
	
	public function getAmount() {
		return $this->_amount;
	}
	
	public function getDescription() {
		return $this->_description;
	}
	
	public function getPrice() {
		return round( $this->_price, 2 );
	}
	
	public function getVat() {
		return $this->_vat;
	}
	
	public function getSubtotal() {
		return round( $this->getPrice() * $this->_amount, 2 );
	}
	
	public function getTotal() {
		return round( $this->getSubtotal() + $this->getTotalVat(), 2 );
	}
	
	public function getTotalVat() {
		return round( $this->getPrice() * $this->_vat * $this->_amount, 2 );
	}
	
	public function printAmount() {
		if( $this->_amount==0 && $this->_price==0 )
			return '';
		else
			return number_format($this->_amount, 0, ',', ' ');
	}
	
	public function printSubtotal() {
		if( $this->_amount==0 && $this->_price==0 )
			return '';
		else
			return number_format($this->getSubtotal(), 2, ',', ' ');
	}
	
	public function printTotal() {
		if( $this->_amount==0 && $this->_price==0 )
			return '';
		else
			return number_format($this->getTotal(), 2, ',', ' ');
	}
	
	public function printPrice() {
		if( $this->_amount==0 && $this->_price==0 )
			return '';
		else
			return number_format($this->getPrice(), 2, ',', ' ');
	}
	
	public function printVat() {
		if( $this->_amount==0 && $this->_price==0 )
			return '';
		else
			return $this->getVat()*100 . "%";
	}
	
	public function save( $parent ){
		$db = DB::get();

		$data = array( 	'parent' => $parent,
						'amount' => $this->_amount, 
						'price' => $this->_price, 
						'description' => $this->_description,
						'vat' => ($this->_vat*100) ); //store in percents
		
		if( !isset($this->_id) )
			$db->save( 'purchases', $data );
		else
			$db->update( 'purchases', $data, "id='".$this->_id."'" );
	}
}
?>
